<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<table id="role_table">
    <thead>
    <th data-options="field:'id'">ID</th>
    <th data-options="field:'name'">角色</th>
    <th data-options="field:'remark'">备注</th>
    <th data-options="field:'status'">状态</th>
    </thead>
</table>
<div id="role_tool">
    <a href="#" class="easy-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="doRoleAdd()">添加</a>
    <a href="#" class="easy-linkbutton" data-options="iconCls:'icon-save',plain:true" onclick="doRoleUpdate()">修改</a>
    <a href="#" class="easy-linkbutton" data-options="iconCls:'icon-edit',plain:true" onclick="doMenuAssign()">分配菜单</a>
</div>
<div id="role_dialog" class="easyui-dialog" title="编辑角色"
     data-options="modal:true,closed:true,href:'<%=request.getContextPath()%>/role.html?act=go_edit',footer:'#role_buttons'"
     style="width: 400px;height: 300px">
    s
</div>
<div id="role_menu_dialog" class="easyui-dialog" title="分配权限"
     data-options="modal:true,closed:true,footer:'#role_menu_buttons'" style="width: 400px;height: 300px">
    <ul id="role_menu_tree" class="easyui-tree">

    </ul>
</div>
<div id="role_buttons" style="padding: 5px 15px 5px 0px;text-align: right">
    <a href="#" class="easy-linkbutton" data-options="iconCls:'icon-ok'" onclick="submitRoleForm()">提交</a>
    <a href="#" class="easy-linkbutton" onclick="doCancel('role_dialog')">取消</a>
</div>
<div id="role_menu_buttons" style="padding: 5px 15px 5px 0px;text-align: right">
    <a href="#" class="easy-linkbutton" data-options="iconCls:'icon-ok'" onclick="submitRoleMenu()">提交</a>
    <a href="#" class="easy-linkbutton" onclick="doCancel('role_menu_dialog')">取消</a>
</div>
<script type="text/javascript">
    $(function () {
        $("#role_table").datagrid({
            url:"<%=request.getContextPath()%>/role.html?act=list",
            singleSelect:true,
            toolbar:"#role_tool"
        })
    });

    function doRoleAdd() {
        $("#role_dialog").dialog("open");
    }
    function doCancel(domId) {
        $("#"+domId).dialog("close")
    }

    function submitRoleForm() {
        $.ajax({
            url:"<%=request.getContextPath()%>/role.html?act=edit",
            data:$("#role_form").serialize(),
            mothod:"post",
            success:function (data) {
                if (data.status){
                    $("#role_dialog").dialog("close");
                    $("#role_table").datagrid("load");
                }else {
                    alert(data.message);
                }
            }
        })
    }

    function doRoleUpdate() {
        var row = $("#role_table").datagrid("getSelected");
        if (row == null || row.length == 0){
            return;
        }
        $("#role_dialog").dialog({
            onLoad:function () {
                $("#role_form").form("load",{
                    id:row.id,
                    name:row.name,
                    remark:row.remark,
                    status:row.status
                })
            }
        }).dialog("open");
    }

    function doMenuAssign() {
        var row = $("#role_table").datagrid("getSelected");
        if (row == null || row.length == 0){
            return;
        }
        $.ajax({
            url:"<%=request.getContextPath()%>/role.html?act=role_menu",
            data:{
                roleId:row.id
            },
            mothod:"post",
            success:function (menuIds) {
                $("#role_menu_tree").tree({
                    url:"<%=request.getContextPath()%>/menu.html?act=list",
                    mathod:"post",
                    checkbox:true,
                    onLoadSuccess:function (node,data) {
                        for(var i =0;i < menuIds.length;i++){
                            var nodeMenu = $("#role_menu_tree").tree("find",menuIds[i]);
//                            if($("#role_menu_tree").tree("getChildren",nodeMenu.length < 1)){
                            if($("#role_menu_tree").tree("isLeaf",nodeMenu.target)){
                                $("#role_menu_tree").tree("check",nodeMenu.target);
                            }
                        }
                        $("#role_menu_dialog").dialog("open")
                    }
                })
            }
        })

    }
    function submitRoleMenu() {
        var row = $("#role_table").datagrid("getSelected");
        if (row == null || row.length == 0){
            return;
        }
        var menuIds = new Array();
        $($("#role_menu_tree").tree("getChecked")).each(function (i,node) {
            menuIds[i] = node["id"]
        })
        $.ajax({
            url:"<%=request.getContextPath()%>/role.html?act=assign_menu",
            data:{
                roleId:row["id"],
                menuIds:menuIds.join(",")
            },
            mothod:"post",
            success:function (data) {
                if (data.status){
                    $("#role_menu_dialog").dialog("close");
                }else {
                    alert(data.message);
                }
            }
        })
    }
</script>
